跳到主要内容

Stable Diffusion 的 API 调用

前置说明

首先修改 webui-user.bat 脚本

@echo off

set PYTHON=
set GIT=
set VENV_DIR=
set COMMANDLINE_ARGS=--xformers

call webui.bat

Stable Diffusion 的 API 是基于 HTTP 协议的,项目启动后,可以访问下面的地址看到这个 API 的文档

http://127.0.0.1:7860/docs

sd 官方提供的 api 常用的有几个:

/sdapi/v1/txt2img 文字生图 POST
/sdapi/v1/img2img 图片生图 POST
/sdapi/v1/options 获取设置 GET | 更新设置 POST(可用来更新远端的模型)
/sdapi/v1/sd-models 获取所有的模型 GET

取得模型列表

GET  http://127.0.0.1:7860/sdapi/v1/sd-models

文生图

http://127.0.0.1:7860/sdapi/v1/txt2img
{
"denoising_strength": 0,
"prompt": "puppy dogs", //提示词
"negative_prompt": "", //反向提示词
"seed": -1, //种子,随机数
"batch_size": 2, //每次张数
"n_iter": 1, //生成批次
"steps": 50, //生成步数
"cfg_scale": 7, //关键词相关性
"width": 512, //宽度
"height": 512, //高度
"restore_faces": false, //脸部修复
"tiling": false, //可平埔
"override_settings": {
"sd_model_checkpoint": "wlop-any.ckpt [7331f3bc87]"
}, // 一般用于修改本次的生成图片的 stable diffusion 模型,用法需保持一致
"script_args": [
0,
true,
true,
"LoRA",
"dingzhenlora_v1(fa7c1732cc95)",
1,
1
], // 一般用于lora模型或其他插件参数,如示例,我放入了一个lora模型, 1,1为两个权重值,一般只用到前面的权重值1
"sampler_index": "Euler" //采样方法
}

注意它生成的图片是 Base64 编码的,需要解码后才能看到图片

References